package com.heima.article.listner;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constant.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import com.heima.model.mess.UpdateArticleMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleIncrHandleListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC)
    public void onMessage(String mess){
        if(StringUtils.isNotBlank(mess)){
            log.info("收到文章消息，准备修改文章行为数据：{}",mess);
            UpdateArticleMess updateArticleMess = JSON.parseObject(mess, UpdateArticleMess.class);
            ArticleVisitStreamMess articleVisitStreamMess = JSON.parseObject(mess, ArticleVisitStreamMess.class);
            switch (updateArticleMess.getType().name()){
                case "COLLECTION":
                    articleVisitStreamMess.setCollect(updateArticleMess.getAdd());
                    break;
                case "COMMENT":
                    articleVisitStreamMess.setComment(updateArticleMess.getAdd());
                    break;
                case "LIKES":
                    articleVisitStreamMess.setLike(updateArticleMess.getAdd());
                    break;
                case "VIEWS":
                    articleVisitStreamMess.setView(updateArticleMess.getAdd());
            }
            apArticleService.updateScore(articleVisitStreamMess);

        }
    }
}